<?php
class Administrator_DistrictController extends Zend_Controller_Action
{
	public function init() {
		
	}
	
	public function indexAction(){
		$objDistrict = new HT_Model_administrator_models_district();
		$do = @$this->_request->getParam('do');
		$district_id = (int)$this->_request->getParam('id');
		if($do == 'delete' && $district_id >0){
			$this->deleteDistrict($district_id);
		}elseif($do == 'list'){
			$this->getListDistrict();
		}elseif($do == 'getDistrict'){
			$province_id = (int)$this->_request->getParam('province_id');
			echo $this->getDistrict($province_id); die;
			break;
		}else{
			$objUtil 			= new HT_Model_administrator_models_utility();
			$province 			= $objUtil->GetCombobox('province_id','province_id','province_name','province',array('isBlankVal'=>'Vui lòng chọn tỉnh/thành phố'));
			
			$this->view->province  		= $province;
		}
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/district/index.js');
	}

	public function updateAction(){
		$objDistrict 	= new HT_Model_administrator_models_district();
		$objUtil 		= new HT_Model_administrator_models_utility();
		$do 		 	= @$this->_request->getParam('do');
		$district_id 	= (int)$this->_request->getParam('id');
		$status 		= (int)$this->_request->getParam('status');
		$province_id 	= null;
		if($do == 'submit'){
			$data = array();
			$data['district_name'] 		= $this->_request->getParam('district_name');
			$data['province_id'] 		= (int)$this->_request->getParam('province_id');
			if($district_id >0){
				$status = $objDistrict->updateDistrict($data,$district_id);
				if($status >0){
					$this->_redirect(WEB_PATH.'/administrator/district');
				}else{
					$this->_redirect(WEB_PATH."/administrator/district/update?status=$status&id=$district_id");
				}
			}else{
				$status = $objDistrict->addDistrict($data);
				if($status >0){
					$this->_redirect(WEB_PATH.'/administrator/district');
				}else{
					$this->_redirect(WEB_PATH."/administrator/district/update?status=$status");
				}
			}
		}elseif($district_id >0){
			$district 				 = $objDistrict->getDistrict($district_id);
			$province_id 			 = $district['province_id'];
			$this->view->district    = $district;
		}
		$province 	= $objUtil->GetCombobox('province_id','province_id','province_name','province',array('defaultValue'=>$province_id,'isBlankVal'=>'Vui lòng chọn tỉnh/thành phố'));
		
		$this->view->id 			= $district_id;
		$this->view->status 	 	= $status;
		$this->view->province  		= $province;
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/district/update.js');
	}

	function deleteDistrict($district_id){
		$objDistrict = new HT_Model_administrator_models_district();
		echo $objDistrict->delete("district_id=".(int)$district_id);die();
	}

	function getListDistrict(){
		$objUtil 		= new HT_Model_administrator_models_utility();
		$objDistrict 	= new HT_Model_administrator_models_district();
		$keyword 		= trim($this->_request->getParam('keyword'));
		$page 			= (int)$this->_request->getParam('page');
		$province_id 		= (int)$this->_request->getParam('province_id');
		$district_id 		= (int)$this->_request->getParam('district_id');
		$size 				= PAGING_SIZE;
		if (!is_numeric($page) || $page <= 0) {
			$page = 1;
		}
		
		$filter = array();
		if($keyword) $filter['keyword'] 			= $keyword;
		if($province_id) $filter['province_id'] 	= $province_id;
		if($district_id) $filter['district_id'] 	= $district_id;
		
		$start = $page * $size - $size;
		$totalRecord = $objDistrict->getListDistrict_nb($filter);
		$listDistrict = $objDistrict->getListDistrict($start,$size,$filter);
		$paging = trim($objUtil->paging($page, $size, $totalRecord));

		$ajaxData = null;
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
		$ajaxData .= '<table cellspacing="0" class="tablesorter tablesorterBorder">';
		$ajaxData .= '<thead>';
			$ajaxData .= '<tr>';
				$ajaxData .= '<th width="15">STT</th>';
				$ajaxData .= '<th width="450">Quận - Huyện</th>';
				$ajaxData .= '<th width="450">Tỉnh - Thành phố</th>';
				$ajaxData .= '<th style="white-space: nowrap;padding-right: 5px;" align="center">Điều khiển</th>';
			$ajaxData .= '</tr>';
		$ajaxData .= '</thead>';
		
		$i=0;
		foreach($listDistrict as $district){
			$i++;
			$trClass = null;
			if($i%2 == 1) $trClass = ' class="altrow"';
			$ajaxData .= '<tr id="'.$district['district_id'].'" '.$trClass.'>';
			$ajaxData .= '<td align="center">'.$i.'</td>';
			$ajaxData .= '<td><a href="'.WEB_PATH.'/administrator/district/update/?id='.$district['district_id'].'">'.$objUtil->tooltipString($district['district_name'],200).'</a></td>';
			$ajaxData .= '<td>'.$district['province_name'].'</td>';
			$ajaxData .= '<td style="white-space: nowrap" align="center">';
			$ajaxData .= '<a href="#" onclick="deleteDistrict('.$district['district_id'].')">Xóa</a> | <a href="'.WEB_PATH.'/administrator/district/update/?id='.$district['district_id'].'">Sửa</a>';
			$ajaxData .= '</td>';
			$ajaxData .= '</tr>';
		}
		$ajaxData .= '</tbody>';
		$ajaxData .= '</table>';
		$ajaxData .= '</div>';
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
		echo $ajaxData; die();
	}
}
